package cn.w.outdoor.lib.media;

import android.view.SurfaceView;

import java.util.concurrent.atomic.AtomicBoolean;

import cn.w.outdoor.lib.model.Trans_Package;

public class H264VideoPlayer {

    public OnDataListener getDataListener() {
        if (videoDecoderAndDisplayThread != null) {
            return videoDecoderAndDisplayThread;
        }
        return null;
    }

    private final static String TAG = "VideoPlayer";

    private final AtomicBoolean control = new AtomicBoolean(false);

    private VideoDecoderAndDisplayThread videoDecoderAndDisplayThread = null;

    private boolean enableData = false;

    public void startVideoDecoder(final SurfaceView surfaceView) {
        synchronized (control) {
            if (!control.get()) {
                if (videoDecoderAndDisplayThread == null) {
                    videoDecoderAndDisplayThread = new VideoDecoderAndDisplayThread(surfaceView);
                    videoDecoderAndDisplayThread.start();
                    enableData = true;
                    control.set(true);
                }
            }
        }
    }

    public void stopVideoDecoder() {
        synchronized (control) {
            if (control.get()) {
                if (videoDecoderAndDisplayThread != null) {
                    enableData = false;
                    videoDecoderAndDisplayThread.stopEncoder();
                    try {
                        videoDecoderAndDisplayThread.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    videoDecoderAndDisplayThread = null;
                }
            }
        }
    }

}
